package com.woorigil.social.domain.stats;

import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woorigil.social.DateUtils;
import com.woorigil.social.domain.landing.LandingCondition;
import com.woorigil.social.domain.landing.LandingDao;
import com.woorigil.social.domain.stats.ActionStats.Data;

@Service
public class StatsService {
	@Autowired LandingDao landingDao;
	
	public ActionStats getActionStats(int days) {
		ActionStats actionStats = new ActionStats();
		for (int i=days; i>=0; i--) {
			Calendar today = Calendar.getInstance();
			today.add(Calendar.DATE, -(i));
			String date = DateUtils.toString(today.getTime(), "yyyyMMdd");
			
			LandingCondition condition = new LandingCondition(date);
			condition.setTodayFormat(LandingCondition.DAY_FORMAT);
			int userCount = landingDao.selectUserCount(condition);
			int landingCount = landingDao.selectLandingCount(condition);
			actionStats.addData(new Data(DateUtils.toString(today.getTime(), "MM/dd"), userCount, landingCount));
		}
		return actionStats;
	}
	
	public ActionStats getActionStatsByTime(int days) {
		ActionStats actionStats = new ActionStats();
		for (int i=days; i>=0; i--) {
			Calendar today = Calendar.getInstance();
			today.add(Calendar.DATE, -(i));
			String date = DateUtils.toString(today.getTime(), "yyyyMMdd");
			
			LandingCondition condition = new LandingCondition(date);
			condition.setTodayFormat(LandingCondition.DAY_FORMAT);
			int userCount = landingDao.selectUserCount(condition);
			int landingCount = landingDao.selectLandingCount(condition);
			actionStats.addData(new Data(DateUtils.toString(today.getTime(), "MM/dd"), userCount, landingCount));
		}
		return actionStats;
	}
}
